﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Bingosoft.Web.Utils;

namespace Bingosoft.WebApp.Shared.UserControls
{
    public partial class GridViewSearch : UserControl
    {
        #region public event
        private static readonly object EventSearching = new object();
        private static readonly object EventSearched = new object();
        public event EventHandler Searching
        {
            add
            {
                base.Events.AddHandler(EventSearching, value);
            }
            remove
            {
                base.Events.RemoveHandler(EventSearching, value);
            }
        }
        public event EventHandler Searched
        {
            add
            {
                base.Events.AddHandler(EventSearched, value);
            }
            remove
            {
                base.Events.RemoveHandler(EventSearched, value);
            }
        }

        private static readonly object EventRefreshing = new object();
        private static readonly object EventRefreshed = new object();
        public event EventHandler Refreshing
        {
            add
            {
                base.Events.AddHandler(EventRefreshing, value);
            }
            remove
            {
                base.Events.RemoveHandler(EventRefreshing, value);
            }
        }
        public event EventHandler Refreshed
        {
            add
            {
                base.Events.AddHandler(EventRefreshed, value);
            }
            remove
            {
                base.Events.RemoveHandler(EventRefreshed, value);
            }
        }

        private static readonly object EventResetting = new object();
        private static readonly object EventResetted = new object();
        public event EventHandler Resetting
        {
            add
            {
                base.Events.AddHandler(EventResetting, value);
            }
            remove
            {
                base.Events.RemoveHandler(EventResetting, value);
            }
        }
        public event EventHandler Resetted
        {
            add
            {
                base.Events.AddHandler(EventResetted, value);
            }
            remove
            {
                base.Events.RemoveHandler(EventResetted, value);
            }
        }


        protected virtual void OnSearching(Object sender, EventArgs eventArgs)
        {
            EventHandler handler = base.Events[EventSearching] as EventHandler;
            if (handler != null)
            {
                handler(sender, eventArgs);
            }
        }

        protected virtual void OnSearched(Object sender, EventArgs eventArgs)
        {
            EventHandler handler = base.Events[EventSearched] as EventHandler;
            if (handler != null)
            {
                handler(sender, eventArgs);
            }
        }

        protected virtual void OnRefreshing(Object sender, EventArgs eventArgs)
        {
            EventHandler handler = base.Events[EventRefreshing] as EventHandler;
            if (handler != null)
            {
                handler(sender, eventArgs);
            }
        }

        protected virtual void OnRefreshed(Object sender, EventArgs eventArgs)
        {
            EventHandler handler = base.Events[EventRefreshed] as EventHandler;
            if (handler != null)
            {
                handler(sender, eventArgs);
            }
        }

        protected virtual void OnResetting(Object sender, EventArgs eventArgs)
        {
            EventHandler handler = base.Events[EventResetting] as EventHandler;
            if (handler != null)
            {
                handler(sender, eventArgs);
            }
        }

        protected virtual void OnResetted(Object sender, EventArgs eventArgs)
        {
            EventHandler handler = base.Events[EventResetted] as EventHandler;
            if (handler != null)
            {
                handler(sender, eventArgs);
            }
        }

        public delegate void SearchEventHandler();
        public event SearchEventHandler SearchReplenish;

        public delegate void ResetEventHandler();
        public event ResetEventHandler ResetReplenish;

        #endregion

        #region public property

        public String GridViewId { get; set; }

        [TemplateContainer(typeof(SearchContainer))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate SearchConditionTemplate { get; set; }

        private bool _showRestButton = true;
        /// <summary>
        /// 是否现实HiddenField
        /// </summary>
        [PersistenceMode(PersistenceMode.Attribute)]
        public bool ShowRestButton
        {
            get { return _showRestButton; }
            set { _showRestButton = value; }
        }


        /// <summary>
        /// Reset时是否重置HiddenField域
        /// </summary>
        [PersistenceMode(PersistenceMode.Attribute)]
        public bool ResetHiddenField { get; set; }

        #endregion

        #region public method

        /// <summary>
        /// 查询列表，pageIndex重置0
        /// </summary>
        public void Search()
        {
            if (!String.IsNullOrEmpty(GridViewId))
            {
                Control ctr = ControlHelper.FindControl(this.Page, GridViewId);

                if (ctr is GridView)
                {
                    OnSearching(this, EventArgs.Empty);

                    ((GridView)ctr).PageIndex = 0;
                    ctr.DataBind();

                    OnSearched(this, EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// 刷新列表，pageIndex不变
        /// </summary>
        public void Refresh()
        {
            if (!String.IsNullOrEmpty(GridViewId))
            {
                Control ctr = ControlHelper.FindControl(this.Page, GridViewId);

                if (ctr is GridView)
                {
                    OnRefreshing(this, EventArgs.Empty);

                    ctr.DataBind();

                    OnRefreshed(this, EventArgs.Empty);
                }
            }
        }        

        #endregion

        #region

        public GridViewSearch()
        {
            ResetHiddenField = false;
        }

        protected void Page_Init()
        {
            if (SearchConditionTemplate != null)
            {
                var container = new SearchContainer();
                SearchConditionTemplate.InstantiateIn(container);
                GridViewSearch_PlaceHolder.Controls.Add(container);
            }

            this.btnRest.Visible = this.ShowRestButton;
        }

        /// <summary>
        /// 重新查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void Search(object sender, EventArgs e)
        {
            Search();
            if (SearchReplenish != null)
            {
                SearchReplenish();
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void Reset(object sender, EventArgs e)
        {
            ResetControl(this);

            OnResetting(this, EventArgs.Empty);

            if (ResetReplenish != null)
            {
                ResetReplenish();
            }
            Search();

            OnResetted(this, EventArgs.Empty);
        }        

        private void ResetControl(Control control)
        {
            if (control != null)
            {
                if (control is HtmlInputControl)
                {
                    ((HtmlInputControl)control).Value = string.Empty;
                }
                else if (control is HtmlTextArea)
                {
                    ((HtmlTextArea)control).Value = string.Empty;
                }
                else if (control is TextBox)
                {
                    ((TextBox)control).Text = string.Empty;
                }
                else if (control is DropDownList)
                {
                    ((DropDownList)control).SelectedIndex = 0;
                }
                else if (control is ListControl)
                {
                    ((ListControl)control).SelectedIndex = 0;
                }
                else if (control is HiddenField && ResetHiddenField)
                {
                    ((HiddenField)control).Value = string.Empty;
                }

                if (control.HasControls())
                {
                    foreach (Control child in control.Controls)
                    {
                        ResetControl(child);
                    }
                }
            }
        }
        #endregion               
    }

    public class SearchContainer : Control, INamingContainer
    {
        internal SearchContainer()
        {
        }
    }
}